package com.open.pokergames.biz.views.groups

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.jiangyy.easydialog.CommonDialog
import com.open.pokergames.R
import com.open.pokergames.base.activity.EventBusTitleBaseActivity
import com.open.pokergames.biz.entity.GroupDto
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_group_detail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GroupDetailActivity : EventBusTitleBaseActivity(), View.OnClickListener {
    private var groupId: String? = ""
    private var groupRole = 0
    private var viewModel: BizViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_group_detail)
        groupId = intent.getStringExtra("groupId")
        setViewTitle("俱乐部信息")
        viewModel = BizViewModel()

        viewModel!!.groupDetail(groupId!!)
        setListener()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun callback(resultInfo: ResultInfo<GroupDto>) {
        if (resultInfo.eventType != "groupDetail") {
            return
        }

        hideLoadingDialog()

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                show(resultInfo.data)
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun show(dataInfo: GroupDto) {
        val data = dataInfo.groupEntity
        groupNameTv.text = XUtils.string().getNotNullValue(data.groupName)
        groupNoTv.text = "ID: " + XUtils.string().getNotNullValue(data.groupNo)
        localTv.text = XUtils.string().getNotNullValue(data.address)
        descTv.text = XUtils.string().getNotNullValue(data.groupDesc)
        groupMembersTv.text = "${data.groupMemberSize}/${data.groupMemberMax}"

        linkTypeValueTv.text = "${XUtils.string().getNotNullValue(data.linkType)} ${XUtils.string().getNotNullValue(data.linkValue)}"

        if (dataInfo.isHasJoined) {
            exitGroupBtn.visibility = View.VISIBLE
        } else {
            exitGroupBtn.visibility = View.GONE
        }

        // 显示控制
        groupRole = dataInfo.groupMember.groupRole;
        if (dataInfo.groupMember != null) {
            when (groupRole) {
                3 -> {// 群成员按钮 显示控制
                    groupMemberItem.visibility = View.VISIBLE

                }
                5 -> {// 上分按钮 显示控制
                    groupMemberItem.visibility = View.VISIBLE
                    addGroupCoinItem.visibility = View.VISIBLE
                }
                else -> {
                    groupMemberItem.visibility = View.GONE
                    addGroupCoinItem.visibility = View.GONE
                }
            }
        } else {
            groupMemberItem.visibility = View.GONE
            addGroupCoinItem.visibility = View.GONE
        }
    }

    private fun setListener() {
        groupMemberItem.setOnClickListener(this)
        addGroupCoinItem.setOnClickListener(this)

        exitGroupBtn.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v) {
            groupMemberItem -> {
                XUtils.intent().openActivity(this@GroupDetailActivity, GroupMemberActivity::class.java)
                    .putStringExtra("groupId", groupId)
                    .putIntExtra("groupRole", groupRole)
                    .start()
            }
            addGroupCoinItem -> {
                XUtils.intent().openActivity(this@GroupDetailActivity, GroupCoinsActivity::class.java)
                    .putStringExtra("groupId", groupId)
                    .start()
            }
            exitGroupBtn -> {
                CommonDialog.Builder(this)
                    .setCancelable(false)
                    .setTitle("退出俱乐部")
                    .setMessage("确定当前俱乐部吗？")
                    .setPositiveButton("确定退出") {
                        showLoadingDialog("正在退出")
                        viewModel!!.inviteExit(groupId!!, "俱乐部")

                    }.setNegativeButton("取消") {
                    }.show()
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun inviteExitGroupBusCallback(result: ResultInfo<String>) {

        if ("inviteExit" != result.eventType) {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("退出成功")
                setResult(200)
                finish()
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}